//
//  QJDataHandle.m
//  QJWork
//
//  Created by 高中雷 on 15/5/30.
//  Copyright (c) 2015年 高中雷. All rights reserved.
//

#import "QJDataHandle.h"

@implementation QJDataHandle

+ (QJDataHandle *)sharedInstance
{
    static QJDataHandle  * sharedInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[self alloc] init];
    });
    return sharedInstance;
}

#pragma mark - 数据处理
- (void)dataMapModel:(id)responseObj modelObject:(id)objectClass handle:(QJBlockRequst)handle{
    if(responseObj){
        if ([responseObj objectForKey:@"respDesc"] && [[responseObj objectForKey:@"respCode"] integerValue] !=0) {
            
            //返回错误信息 根据需求在修改
            handle(NULL,responseObj);
            
                if([[responseObj objectForKey:@"respCode"]  isEqualToString:@"99"]){
                    NSLog(@"重新登录");
//                    [self handleReLogin];
                }else if([[responseObj objectForKey:@"respCode"] isEqualToString:@"203"]){
                    [self fangWenCiSAlt];
                    return ;
                }
           
        }else{
            reLoginAlertView = nil;
            if(objectClass==nil){
                //不要映射 数据类型已经是字典了
                handle(responseObj,NULL);
            }else{
                //映射模型对象
                id class = nil;
                if([objectClass isKindOfClass:[NSString class]]||[objectClass isKindOfClass:[NSObject class]]){
                    if(responseObj && [responseObj isKindOfClass:[NSDictionary class]]){
        
                        class  = [NSClassFromString(objectClass) objectWithKeyValues:(NSDictionary *)responseObj];
                        
                    }else if(responseObj && [responseObj isKindOfClass:[NSArray class]]){
                        
                        class  = [NSClassFromString(objectClass) objectArrayWithKeyValuesArray:(NSDictionary *)responseObj];
                    }
                }
//                NSLog(@"原始数据::::::::%@",(NSDictionary *)responseObj);
                NSLog(@"::::::::::::::::::::::::::::::::::::::::ent:::::::::::::::::::::::::::::::::::::::::::");
                handle(class,NULL);
                
            }
        }
    }
}


-(void)fangWenCiSAlt{
    UIAlertView *alt = [[UIAlertView alloc]initWithTitle:nil message:@"您今天访问的次数太多了，请明天再来吧！" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    alt.tag = 11;
    [alt show];
}

//- (void)handleReLogin{
//    if(!reLoginAlertView){
//        reLoginAlertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"请重新登录" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        reLoginAlertView.tag = 10;
//        [reLoginAlertView show];
//        
//        UserInfoManager * manager = [UserInfoManager shareManager];
//        [DeleteModel deleteAllCardsUserDefaults];
//        //删除个人信息
//        DataCenter * dataBase = [DataCenter sharedInstanceCreateDataBase];
//        [dataBase deleteUserInfoWithUserId:manager.userModel.userId];
//        
//        manager.userModel.userId = @"";
//        
//        //注销当前账号
//        NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
//        [user removeObjectForKey:nowIsLogin];
//        
//        
//        NSData * data = [NSKeyedArchiver archivedDataWithRootObject:manager.userModel];
//        [user setObject:data forKey:@"userInfoData"];
//        
//        [user synchronize];
//        
//        //跳转至登录界面
//        NSNotificationCenter * defaultCenter = [NSNotificationCenter defaultCenter];
//        [defaultCenter postNotificationName:@"createWelcomeLastPage" object:nil];
//    }
//    
//    
//}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if(alertView.tag==11){
        [[NSNotificationCenter defaultCenter]postNotificationName:@"blockNotification" object:nil];
    }
}


#pragma mark - 服务器返回错误信息处理
+(NSDictionary*)errorCode:(int)nCode withString:(NSString*)strError withData:(NSString*)strData withHttpCode:(int)nHttpCode {
    if (strData)
        return @{ @"errorCode": [NSNumber numberWithInt:nCode], @"errorString" : strError, @"errorData" : strData, @"httpCode" : [NSNumber numberWithInt:nHttpCode] };
    return @{ @"errorCode": [NSNumber numberWithInt:nCode], @"errorString" : strError, @"httpCode" : [NSNumber numberWithInt:nHttpCode] };
}

#pragma mark - 接口调用返回错误信息,根据需求进行修改
+(void)error:(NSError*)error {
    
    switch ([error code]) {
        case NSURLErrorTimedOut:
            NSLog(@"网络请求超时");
            break;
        case NSURLErrorNotConnectedToInternet:
            NSLog(@"请检查网络连接");
            break;
        default:
            NSLog(@"连接不到网络服务");
            break;
    }
}
-(BOOL)checkNetwork:(AFNetworkReachabilityStatus)status{
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWiFi:
            NSLog(@"无线网络");
            return YES;
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            NSLog(@"3G网络");
            return YES;
            break;
        case AFNetworkReachabilityStatusNotReachable:
            NSLog(@"未连接");
            [self showAlt];
            return NO;
            break;
        case AFNetworkReachabilityStatusUnknown:
            NSLog(@"未知错误");
            
            return NO;
            break;
    }
}



-(void)showAlt{
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
//    hud.mode = MBProgressHUDModeText;
//    hud.labelText = @"无网络";
//    hud.margin = 10.f;
//    hud.removeFromSuperViewOnHide = YES;
//    [hud hide:YES afterDelay:3];
}



@end
